<?php
namespace common\models;

use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use api\components\OperateException;
use Yii;

/**
 * 币记录数据模型
 * @author CGA
 */
class ARCoinRecord extends ActiveRecord
{
    const TYPE_IN = 1;
    const TYPE_OUT = 2;

    const STATUS_UNDETERMINED = 1;
    const STATUS_WAITCONFIRM = 2;
    const STATUS_SUCCESS = 9;
    const STATUS_FAIL = -1;

    public static $categorys = [
        'recharge'          => ['id' => 1, 'type' => self::TYPE_IN], //充值
        'buy'               => ['id' => 2, 'type' => self::TYPE_IN], //买入
        'trade_freeze'      => ['id' => 3, 'type' => self::TYPE_OUT], //交易冻结
        'trade_unfreeze'    => ['id' => 4, 'type' => self::TYPE_IN], //解冻返还
        'restitution'       => ['id' => 5, 'type' => self::TYPE_IN], //获赔收入
        'vouch'             => ['id' => 6, 'type' => self::TYPE_OUT], //担保冻结
        'vouch_return'      => ['id' => 7, 'type' => self::TYPE_IN], //担保返还
        'earn'              => ['id' => 8, 'type' => self::TYPE_IN], //手续费收入
        'withdraw'          => ['id' => 9, 'type' => self::TYPE_OUT], //提出
        'platform_increase' => ['id' => 10, 'type' => self::TYPE_IN], //平台收入
        'platform_reduce'   => ['id' => 11, 'type' => self::TYPE_OUT], //平台支出
    ];

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%coin_record}}';
    }

    /**
     * 获取虚拟币信息
     */
    public function getCoinType()
    {
        return $this->hasOne(ARCoinType::className(), ['id' => 'coin']);
    }

    /**
     * 获取用户信息
     */
    public function getUser()
    {
        return $this->hasOne(ARUser::className(), ['id' => 'uid']);
    }

    /**
     * 添加记录
     * @param nunber $uid        用户编号
     * @param nunber $coin       代币编号
     * @param nunber $amount     数额
     * @param nunber $amount_old 余额
     * @param string $operation  操作
     * @param nunber $serial_id  流水编号
     * @param nunber $opp_uid    对方用户编号
     */
    public static function addRecord($uid, $coin, $amount, $amount_old, $operation, $serial_id = null, $opp_uid = null)
    {
        if (!isset(self::$categorys[$operation])) {
            throw new OperateException('400005');
        } else {
            $operationInfo = self::$categorys[$operation];
            $model = new self();
            $model->type = $operationInfo['type'];
            $model->category = $operationInfo['id'];
            $model->uid = $uid;
            if ($serial_id) {
                $model->serial_id = $serial_id;
            }
            if ($opp_uid) {
                $model->opp_uid = $opp_uid;
            }
            $scale = Yii::$app->params['scale'];
            $model->coin = $coin;
            $model->amount = $amount;
            $model->amount_old = sprintf("%.{$scale}f", $amount_old);
            if ($model->type === self::TYPE_IN) {
                $model->amount_new = bcadd(sprintf("%.{$scale}f", $amount_old), sprintf("%.{$scale}f", $amount), $scale);
            } else {
                $model->amount_new = bcsub(sprintf("%.{$scale}f", $amount_old), sprintf("%.{$scale}f", $amount), $scale);
            }
            $model->status = self::STATUS_UNDETERMINED;
            if ($model->save()) {
                return $model->id;
            } else {
                return false;
            }
        }
    }

    /**
     * 更新记录
     * @param nunber $record_id 记录编号
     * @param nunber $status    记录状态
     * @param string $block_sid 区块链回执
     */
    public static function updateRecord($record_id, $status, $block_sid = null)
    {
        $model = self::findOne($record_id);
        $model->status = $status;
        if ($block_sid) {
            $model->block_sid = $block_sid;
        }
        if ($model->update()) {
            return $model->id;
        } else {
            return false;
        }
    }

    /**
     * 获取类型标签
     * @param string $type 类型
     */
    public static function getCategoryLabel($type = 'all')
    {
        $labels = [
            '1' => '充值',
            '2' => '买入',
            '3' => '交易冻结',
            '4' => '撤销解冻',
            '5' => '获赔收入',
            '6' => '担保冻结',
            '7' => '担保返还',
            '8' => '佣金收入',
            '9' => '提出',
        ];
        if ($type == 'all') {
            $data = $labels;
        } else {
            $data = $labels[$type];
        }
        return $data;
    }

    /**
     * 获取状态标签
     */
    public static function getStatusLabel($status)
    {
        switch ($status) {
            case 1:
                $label = '待审核';
                break;
            case 2:
                $label = '待处理';
                break;
            case 9:
                $label = '已成功';
                break;
            case -1:
                $label = '已失败';
                break;
            default:
                $label = '异常';
                break;
        }
        return $label;
    }
}
